package com.example.foo.admin.web.user;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.user.model.User;
import com.example.foo.user.query.UserQuery;
import com.example.foo.user.service.UserService;

@org.springframework.web.bind.annotation.RestController
public class UserAction {

	private final UserService userService;

	public UserAction(UserService userService) {
		this.userService = userService;
	}

	@GetMapping("/api/users")
	public AjaxResult index(UserQuery userQuery, Pageable pageable) {
		PageResult<User> pageResult = userService.findUsers(userQuery, pageable);
		return AjaxResult.successData(pageResult);
	}

	@GetMapping("/api/users/{id}")
	public AjaxResult showUser(@PathVariable("id") Long id) {
		User user = userService.findUserById(id);
		if (user == null) {
			return AjaxResult.error(404, "Not Found");
		}
		return AjaxResult.successData(user);
	}

}
